package com.example.chongqingichms.restcontroller;

import com.example.chongqingichms.exception.R;
import com.example.chongqingichms.bean.UserBean;
import com.example.chongqingichms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = "/webapi/user")
public class UserRestController {

    @Autowired
    UserService userService;

    @GetMapping("login")
    public R login(String username, String password, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        UserBean userBean = userService.loginIn(username, password);
        if (userBean != null) {
            HttpSession httpSession = httpServletRequest.getSession();
            httpSession.setAttribute("user",userBean);
            return R.ok();
        }
        return R.error();
    }
}
